//
//  TNTraveNotesDetailsTableViewController.m
//  TravelNotes
//
//  Created by 王浩宇 on 15/8/12.
//  Copyright (c) 2015年 王浩宇. All rights reserved.
//

#import "TNTraveNotesDetailsTableViewController.h"
#import <AFNetworking.h>
#import "TNTripDaysModel.h"
#import "TNNotesModel.h"
#import "TNTravelNotesDetailsTableViewCell.h"
#import "Macro.h"
#import "TNNodesModel.h"

@interface TNTraveNotesDetailsTableViewController ()
@property (nonatomic, strong)NSMutableArray *tripDayDetailsArray;
@property (nonatomic, strong)NSMutableArray *notesArray;
@property (nonatomic, strong)NSMutableArray *dayArray;
@property (nonatomic, strong)NSMutableArray *nodesArray;

@end

@implementation TNTraveNotesDetailsTableViewController
- (NSMutableArray *)nodesdetailsArray
{
    if (_tripDayDetailsArray == nil) {
        _tripDayDetailsArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _tripDayDetailsArray;
}
- (NSMutableArray *)notesArray
{
    if (_notesArray == nil) {
        _notesArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _notesArray;
}
- (NSMutableArray *)dayArray
{
    if (_dayArray == nil) {
        _dayArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dayArray;
}
- (NSMutableArray *)nodesArray
{
    if (_nodesArray == nil) {
        _nodesArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _nodesArray;
}

- (void)findData
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *parUrl = [NSString stringWithFormat:@"http://chanyouji.com/api/trips/%@.json",self.ID];
    [manager GET:parUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        
        for (NSDictionary *triDic in dict[@"trip_days"])
        {
            TNTripDaysModel *tripDayModel = [[TNTripDaysModel alloc] init];
            [tripDayModel setValuesForKeysWithDictionary:triDic];
            [self.dayArray addObject:tripDayModel];

        }
        for (TNTripDaysModel *tripDayModel in self.dayArray)
        {
            
            NSArray *arr = tripDayModel.nodesArray;
            for (NSDictionary *nodesDic in arr)
            {
                TNNodesModel *nodesModel = [[TNNodesModel alloc] init];
                [nodesModel setValuesForKeysWithDictionary:nodesDic];
                [self.nodesArray addObject:nodesModel];
                NSArray *notesArr = nodesDic[@"notes"];
                for (NSDictionary *desDic in notesArr)
                {
                    TNNotesModel *notesModel = [[TNNotesModel alloc] init];
                    [notesModel setValuesForKeysWithDictionary:desDic];
                    [self.notesArray addObject:notesModel];
                }
            }
        }
        NSLog(@"dayArray:%@",self.dayArray);
        NSLog(@"notews:%@",self.notesArray);
        NSLog(@"tripday:%@",self.tripDayDetailsArray);
        NSLog(@"nodesArray:%@",self.nodesArray);
        NSLog(@"ID:%@",self.ID);
        NSLog(@"nodesCount:%ld",self.nodesArray.count);
        
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"请求失败");
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self findData];
//    self.tableView.rowHeight = 200;
    [self.tableView registerClass:[TNTravelNotesDetailsTableViewCell class] forCellReuseIdentifier:@"cell"];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dayArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
    TNTripDaysModel *dayModel = _dayArray[section];
    for (NSDictionary *dic in dayModel.nodesArray) {
        TNNodesModel *nodesModel = [[TNNodesModel alloc] init];
        [nodesModel setValuesForKeysWithDictionary:dic];
        for (NSDictionary *nodeDic in nodesModel.notesArray) {
            TNNotesModel *notesModel = [[TNNotesModel alloc] init];
            [notesModel setValuesForKeysWithDictionary:nodeDic];
            [arr addObject:notesModel];
        }
        
    }
    NSLog(@"arr:%@",arr);
    
    
    
    return arr.count;
    
//    TNTripDaysModel *dayModel = _dayArray[section];
//    NSArray *arr = dayModel.nodesArray;
    
//    TNNodesModel *nodesModel = _nodesArray[_dayArray.count];
//    return nodesModel.notesArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TNTravelNotesDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

    TNNodesModel *nodesmodel = _nodesArray[indexPath.section];
    TNNotesModel *model = nodesmodel.notesArray[indexPath.row];
    cell.notesModel = model;

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 500;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
    view.backgroundColor = [UIColor yellowColor];
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
